package com.sc.servlet.user.field;

import com.sc.pojo.BookField;
import com.sc.services.UserServices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: 添加预定信息
 * @author: sc
 * @create: 2023-01-04 21:22
 **/
@WebServlet("/addMes")
public class addMesServlet extends HttpServlet {

    private UserServices userServices = new UserServices();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String result = null;
        request.setCharacterEncoding("UTF-8");
        String userid = request.getParameter("userid");
        String fieldid = request.getParameter("fieldid");
        String fieldname = request.getParameter("fieldname");
        fieldname = new String(fieldname.getBytes("ISO-8859-1"), "utf-8");
        String bookday = request.getParameter("bookday");
        String starttime = request.getParameter("starttime");
        String endtime = request.getParameter("endtime");

        System.out.println(fieldname);


        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String submittime = dateFormat.format(date);
        String fieldbookstate = "审核中";
        SimpleDateFormat dateFormat1= new SimpleDateFormat("yyyyMMddhhmmss");
        String fieldbookid = "bkf"+dateFormat1.format(date);

        BookField bookField = new BookField();
        bookField.setBookday(bookday);
        bookField.setFieldid(Integer.parseInt(fieldid));
        bookField.setFieldbookid(fieldbookid);
        bookField.setFieldname(fieldname);
        bookField.setFieldbookstate(fieldbookstate);
        bookField.setStarttime(starttime);
        bookField.setEndtime(endtime);
        bookField.setSubmittime(submittime);
        bookField.setUserid(userid);
        if(userServices.selectBy(bookField)){
            result = "same_mes";
        }
        else if(userServices.isConflict(bookField)){
            result = "conflict_mes";

        }else {

            userServices.addBookMes(bookField);
            result = "add_suc";
        }

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(result);







    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}